package auth

import (
	"errors"
	"github.com/dgrijalva/jwt-go"
	"time"
)

const (
	secret = "dads&#!@adawdijioj"
	issuer = "mirroruv"
)

type CustomClaims struct {
	UserID string `json:"userID,omitempty"`
	jwt.StandardClaims
}

func GenerateToken(userID string) (string, error) {
	claims := CustomClaims{
		userID,
		jwt.StandardClaims{
			ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
			Issuer:    issuer,
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	jwtSecret := []byte(secret)
	return token.SignedString(jwtSecret)
}
func VerifyToken(tokenString string) (*CustomClaims, error) {
	jwtSecret := []byte(secret)
	token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (any, error) {
		return jwtSecret, nil
	})
	if err != nil {
		return nil, err
	}
	if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
		return claims, nil
	}
	return nil, errors.New("invalid token")
}
